//
//  MSHeroScene.m
//  MagicSword
//
//  Created by Netease on 15/11/16.
//  Copyright © 2015年 Jonear. All rights reserved.
//

#import "MSHeroScene.h"
#import "MSHeroManager.h"
#import "MSHeroInfoNode.h"
#import "MSHeroAvatarNode.h"
#import "MSStoreNode.h"
#import "MSWeaponNode.h"
#import "MSAlertWeaponView.h"
#import "MSWeaponManager.h"
#import "ProgressHUD.h"
#import "MSDBManager.h"

@implementation MSHeroScene {
    MSHeroInfoNode *_heroInfoView;
    MSHeroAvatarNode *_selectedAvatarNode;
    SKSpriteNode *_selectedShowNode;
    NSArray *_heroArray;
    MSStoreNode *_storeView;
    MSAlertWeaponView *_alertView;
}

- (void)didMoveToView:(SKView *)view {
    /* Setup your scene here */
    [super didMoveToView:view];
    [self.topBarNode showBackNode:YES];
    
    [self initHeroListView];
}

- (void)initHeroListView {
    SKSpriteNode *backNode = [SKSpriteNode spriteNodeWithColor:UIColorFromRGBA(0x1d1d1d, 0.66) size:CGSizeMake(ScreenWidth/2, ScreenHeight)];
    backNode.position = CGPointMake(ScreenWidth/4, ScreenHeight/2);
    backNode.zPosition = 2;
    [self addChild:backNode];
    
    _heroArray = [[MSHeroManager shareManager] getAllUserHeroArray];
    int i=0;
    for (MSHeroInfo *hero in _heroArray) {
        MSHeroAvatarNode *heroNode = [[MSHeroAvatarNode alloc] initWithHeroInfo:hero];
        heroNode.position = [self getAvatarPossionWithIndex:i++ size:heroNode.size];
        [heroNode setSelected:hero.isInFight];
        [self addChild:heroNode];
    }
    
    _selectedShowNode = [SKSpriteNode spriteNodeWithImageNamed:@"main_hero_show"];
    _selectedShowNode.zPosition = 4;
    [_selectedShowNode setHidden:YES];
    [self addChild:_selectedShowNode];
    
}

- (BOOL)isLastFightHero {
    if ([self getFightCount] == 1) {
        return YES;
    }
    return NO;
}

- (BOOL)isMaxFightHero {
    if ([self getFightCount] >= [[MSDBManager shareManager] getMaxHeroCount]) {
        return YES;
    }
    return NO;
}

- (NSInteger)getFightCount {
    int count = 0;
    for (MSHeroInfo *hero in _heroArray) {
        if (hero.isInFight) {
            count++;
        }
    }
    return count;
}

- (CGPoint)getAvatarPossionWithIndex:(int)index size:(CGSize)size {
    float space = 13;
    float top = ScreenHeight-80;
    float left = size.width/2+space;
    return CGPointMake(left+((index%5)*(space+size.width)), top-((index/5)*(space+size.height)));
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    [super touchesEnded:touches withEvent:event];
    
    UITouch *touch = [touches anyObject];
    CGPoint positionInScene = [touch locationInNode:self];
    SKSpriteNode *touchedNode = (SKSpriteNode *)[self nodeAtPoint:positionInScene];
    
    if ([touchedNode isKindOfClass:[MSHeroAvatarNode class]]) {
        [self selectAvatarNode:(MSHeroAvatarNode *)touchedNode];
    } else if([touchedNode.parent isKindOfClass:[MSHeroAvatarNode class]]){
        [self selectAvatarNode:(MSHeroAvatarNode *)touchedNode.parent];
    }
    
    // 出战
    if ([[touchedNode name] isEqualToString:NodeName_HeroFigintSeletedNodes]) {
        if (_selectedAvatarNode) {
            if (_selectedAvatarNode.heroInfo.isInFight && [self isLastFightHero]) {
                [ProgressHUD showError:@"至少有个人参加战斗"];
                return;
            } else if (!_selectedAvatarNode.heroInfo.isInFight && [self isMaxFightHero]){
                [ProgressHUD showError:@"目前战斗人数已满"];
                return;
            }
            _selectedAvatarNode.heroInfo.isInFight = !_selectedAvatarNode.heroInfo.isInFight;
            [_selectedAvatarNode setSelected:_selectedAvatarNode.heroInfo.isInFight];
            [_heroInfoView setHeroInfo:[_selectedAvatarNode heroInfo]];
        }
    }
    
    // 选中武器
    if ([[touchedNode name] isEqualToString:NodeName_HeroWeaponNodes] || [[touchedNode name] isEqualToString:NodeName_StoreWeaponNodes]) {
        if (!_alertView) {
            _alertView = [[MSAlertWeaponView alloc] init];
            _alertView.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame));
            _alertView.hidden = YES;
            [self addChild:_alertView];
        }
        
        MSWeaponInfo *weaponInfo = nil;
        if ([touchedNode isKindOfClass:[MSWeaponNode class]]) {
            weaponInfo = [(MSWeaponNode *)touchedNode weaponInfo];
        } else if ([touchedNode.parent isKindOfClass:[MSWeaponNode class]]) {
            weaponInfo = [(MSWeaponNode *)touchedNode.parent weaponInfo];
        }
    
        if (weaponInfo) {
            [_alertView setHidden:NO];
            [_alertView setWeaponInfo:weaponInfo];
        }
        
        if ([[touchedNode name] isEqualToString:NodeName_HeroWeaponNodes]) {
            _alertView.isInHero = YES;
            [_alertView setButtonTitle:@"放回仓库"];
        } else {
            _alertView.isInHero = NO;
            [_alertView setButtonTitle:@"放入背包"];
        }
    }
    
    // 关闭武器窗口
    if ([[touchedNode name] isEqualToString:NodeName_CloseAlertNodes]) {
        if (_alertView) {
            [_alertView setHidden:YES];
        }
    }
    
    // 背包更改
    if ([[touchedNode name] isEqualToString:NodeName_DoneWeaponAlertNodes]) {
        if (_alertView) {
            [_alertView setHidden:YES];
        }
        if (_alertView.weaponInfo) {
            BOOL success = NO;
            if (_alertView.isInHero) {
                success = [[MSHeroManager shareManager] removeHeroWeapon:_selectedAvatarNode.heroInfo weapon:_alertView.weaponInfo];
                if (success) {
                    success = [[MSWeaponManager shareManager] addWeapon:_alertView.weaponInfo];
                }
            } else {
                success = [[MSHeroManager shareManager] addHeroWeapon:_selectedAvatarNode.heroInfo weapon:_alertView.weaponInfo];
                if (success) {
                    success = [[MSWeaponManager shareManager] removeWeapon:_alertView.weaponInfo];
                }
            }
            if (success) {
                [_storeView reloadView];
            } else {
//                [ProgressHUD showError:@"操作失败,背包已满"];
            }
            // 提示
            if (_selectedAvatarNode.heroInfo.weaponList.count == 0) {
                [ProgressHUD showError:@"英雄没有武器在战斗中是无法攻击的!"];
            }
        }
    }
}

- (void)selectAvatarNode:(MSHeroAvatarNode *)avatarNode {
    _selectedAvatarNode = avatarNode;
    
    // 英雄信息
    [self showHeroInfoView];
    [_heroInfoView setHeroInfo:[_selectedAvatarNode heroInfo]];
    
    // 背包消息
    [self showStoreView];
    [_storeView setHeroInfo:[_selectedAvatarNode heroInfo]];
    
    // 显示选中状态
    [_selectedShowNode setHidden:NO];
    [_selectedShowNode setSize:CGSizeMake(_selectedAvatarNode.size.width+2, _selectedAvatarNode.size.height+2)];
    [_selectedShowNode setPosition:_selectedAvatarNode.position];
}

- (void)showHeroInfoView {
    if (!_heroInfoView) {
        _heroInfoView = [[MSHeroInfoNode alloc] init];
        [self addChild:_heroInfoView];
        _heroInfoView.position = CGPointMake(ScreenWidth, ScreenHeight/2);
        SKAction *action = [SKAction moveToX:ScreenWidth/2+ScreenWidth/8 duration:0.2];
        [_heroInfoView runAction:action];
    }
}

- (void)showStoreView {
    if (!_storeView) {
        _storeView = [[MSStoreNode alloc] init];
        [self addChild:_storeView];
        _storeView.position = CGPointMake(ScreenWidth+ScreenWidth/4, ScreenHeight/2);
        SKAction *action = [SKAction moveToX:ScreenWidth/2+ScreenWidth/4+ScreenWidth/8 duration:0.3];
        [_storeView runAction:action];
    }
}


@end
